local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local CallBackManager = CommonOHOH.load.CallBackManager();
local BattleTool=_G.CommonOHOH.load.BattleTool();
local InstanceManager = CommonOHOH.load.InstanceManager();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.car=self.parent
	self.player=self.parent.player
	
    local touchPart=self.car:GetWeapon():GetTouchPart() --获取钻头碰撞题
    print("所有权",touchPart:GetNetworkOwner())
    -- print("_____________",touchPart,self.player)
    -- if touchPart and touchPart:IsA("BasePart") then
    --     local canSet, errorReason = touchPart:CanSetNetworkOwnership()
    --     if canSet then
    --         touchPart:SetNetworkOwner(self.player.main_inst)
    --         print(touchPart:GetFullName() .. "'s Network Ownership can be changed!")
    --     else
    --         warn("Cannot change the Network Ownership of " .. touchPart:GetFullName() .. " because: " .. errorReason)
    --     end
    -- end

	self.CarInfoCtrl=self.player:GetComponent(_G.CommonOHOH.load.Common_CarInfoCtrl())

	CallBackManager:AddRbxConnect(self,touchPart.Touched, function(obj,part)
        if self.dead then
            return
        end
        print("------touchPart.Touched--------",part.name)
        if part.name =="Wall" then
            
            part.Transparency = 1
        end
		local mine= self:FindMine(part)
        if mine then
            print("------touchPart.Touched----Mine----",part.name)
            self:battleWithMine(mine)
        end
	end);

end;

--根据部件找到矿石
function M.FindMine(self,part)
    local m=part:FindFirstAncestorOfClass("Model")
    if m then
        local min= InstanceManager:FindObject(m)
        if min and min.attribute.IsCarTouch then
            return min
        end
    end
end

--计算和矿石的战斗结果
function M.battleWithMine(self,mine)
    local mHp=mine:GetHp()
    local mDamge=mine:GetDamge()
    print("---------------")
if mine.cfg then
    print("---------矿石----hp----damge------",mHp,mDamge,mine.cfg.type)
else
    print("---------宝箱----hp----damge------",mHp,mDamge,mine.itemId)
end
    -- print("---------Mine----hp----damge------",mHp,mDamge,mine.cfg.type)

    local cHp=self.CarInfoCtrl:GetHp()
    local cDamge=self.CarInfoCtrl:GetAttack()


    print("---------Car----hp----damge------",cHp,cDamge)


   local hurt= BattleTool.GetAttackerHurt({
        hp=cHp,
        damge=cDamge,
    },{
        hp=mHp,
        damge=mDamge,
    })

    self.CarInfoCtrl:Hurt(hurt)

    if cHp>hurt then
        mine:Hurt(mHp,self.car:GetAllPlayer())
        self:GetReward(mine:GetItem())
    else
        mine:SetCanCollide(true)
        self:Dead()
    end
    
end

--车子被撞毁
function M.Dead(self)
    if  not self.dead then
        self.dead =true
        self.parent:Boom()
    end
    
--  --待实现....
   
end
--获取奖励
function M.GetReward(self,key,value)

    self.parent.GetRewardEvent:Action(key,value)
end

function M.dtor(self)

	M.super.dtor(self);
end


return M